const Controller = require('egg').Controller;
const crypt = require
class ssh extends Controller {
  /**
   * 打开SSH连接
   */
  async openSSH() {
    const params = {
       host:'180.76.184.253',
       port:22,
       username:'root',
       password:''
    };
    const client = await this.service.ssh.createClient(params);
    const self = this;
    if(client){
      console.info("连接成功")
      client.exec("uname -a",function(err, stream) {
            if(err){
                console.log("-----",err)
                return;
            } 
            stream.on('close', function() {
               console.log('Stream :: close');
               client.end();
            }).on('data', function(data) {
               self.ctx.socket("res",data)
               console.log('STDOUT: ' + data);
            }).stderr.on('data', function(data) {
            console.log('STDERR: ' + data);
            });
            stream.end('ls -l\nexit\n');
        });
    }else{
      console.info("连接失败")
      this.ctx.body=this.ctx.helper.sequence(500,false,`SSH连接失败`)
    }
  }
}
module.exports = ssh;